package com.ce.socket;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import com.ce.study.socketpool.CM20JAVA;

public class SendCallable implements Callable<String> {
	private int execTimes;
	private DalSocketPool pool;
	private Map<String,String> mbody = new HashMap<String, String>();
	private byte[] msg;
	
	public SendCallable(DalSocketPool pool, int execTimes) throws Exception {
		this.pool = pool;
		this.execTimes = execTimes;
		mbody.put("URI", "/dal/testnoauth/fcode.xml");
		mbody.put("METHOD", "GET");
		CM20JAVA reqmsg = new CM20JAVA(mbody);
		reqmsg.encode();
		msg = reqmsg.getMessage();
	}
	
	public String call() throws Exception {
		int succeed = 0;
		int failed = 0;
		for (int i = 0; i < execTimes; i++) {
			try {
				byte[] rep = pool.sendData(msg, 3);
				CM20JAVA repmsg = new CM20JAVA(Map.class);
				repmsg.decode(rep);
				Object repObj = repmsg.getBodyObject();
				//System.out.println(repObj);
				succeed++;
				Thread.sleep(100);
			} catch (DALException e) {
				failed++;
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return execTimes+"|"+succeed+"|"+failed;
	}

}
